package com.yjz.study

import android.os.Bundle
import androidx.annotation.StringRes
import com.yjz.common.tab.FragmentTabView
import com.yjz.common.tab.TabViewAdapter
import com.yjz.study.ui.MainActivity
import com.yjz.study.ui.fragment.HomeFragment
import com.yjz.study.ui.fragment.NormalFragment
import com.yjz.study.ui.fragment.ProfileFragment
import com.yjz.ui.tab.bottom.TabBottomLayout
import com.yjz.ui.tab.common.TabInfo

/**
 * 作用 : 将MainActivity的逻辑内聚在这里，让MainActivity代码更整洁
 * 作者 : yjz
 * 时间 : 2025/2/13 14:58
 */
class MainActivityLogic(private val activityProvider: MainActivity, val savedInstanceState: Bundle?) {

    companion object {
        private const val SAVED_CURRENT_ID: String = "SAVED_CURRENT_ID"
    }

    private var currentItemIndex = 0

    init {
        //fix 不保留活动导致的Fragment重叠问题
        if (savedInstanceState != null) {
            currentItemIndex = savedInstanceState.getInt(SAVED_CURRENT_ID)
        }
        initTabBottom()
    }

    /**
     * 初始化底部的bottom
     */
    private fun initTabBottom() {
        val tabBottomLayout = activityProvider.findViewById<TabBottomLayout>(R.id.tab_layout)
        tabBottomLayout.setTabAlpha(0.85f)
        val bottomInfoList: MutableList<TabInfo<*>> = ArrayList()

        val homeInfo = initBottomTabInfo("首页", R.string.if_home)
        homeInfo.fragment = HomeFragment.newInstance()

        val infoRecommend = initBottomTabInfo("收藏", R.string.if_favorite1)
        infoRecommend.fragment = NormalFragment.newInstance("收藏")

        val infoCategory = initBottomTabInfo("分类", R.string.if_category)
        infoCategory.fragment = NormalFragment.newInstance("分类")

        val infoChat = initBottomTabInfo("推荐", R.string.if_recommend)
        infoChat.fragment = NormalFragment.newInstance("推荐")

        val infoProfile = initBottomTabInfo("我的", R.string.if_profile)
        infoProfile.fragment = ProfileFragment()

        bottomInfoList.add(homeInfo)
        bottomInfoList.add(infoRecommend)
        bottomInfoList.add(infoCategory)
        bottomInfoList.add(infoChat)
        bottomInfoList.add(infoProfile)
        tabBottomLayout.inflateInfo(bottomInfoList)

        val fragmentTabView = activityProvider.findViewById<FragmentTabView>(R.id.fragment_tab_view)
        val adapter = TabViewAdapter(bottomInfoList, activityProvider.supportFragmentManager)
        fragmentTabView.adapter = adapter

        // 选中监听回调
        tabBottomLayout.addTabSelectChangeListener { index, _, _ ->
            fragmentTabView.setCurrentItem(index)
            currentItemIndex = index
        }

        tabBottomLayout.defaultSelected(bottomInfoList[currentItemIndex])
    }

    private fun initBottomTabInfo(title: String, @StringRes iconStr: Int): TabInfo<String> {
        return TabInfo(
            title,
            "fonts/iconfont.ttf",
            activityProvider.getString(iconStr),
            null,
            "#ff656667",
            "#ffd44949"
        )
    }

    /**
     * kill 时保存当前fragment的index
     */
    fun onSaveInstanceState(outState: Bundle) {
        outState.putInt(SAVED_CURRENT_ID, currentItemIndex)
    }
}
